package cn.tedu.sgs.record.controller;

import cn.tedu.sgs.base.response.JsonResult;
import cn.tedu.sgs.record.pojo.dto.RecordSaveParam;
import cn.tedu.sgs.record.pojo.dto.RecordQuery;
import cn.tedu.sgs.record.pojo.dto.RecordReviewDTO;
import cn.tedu.sgs.record.pojo.vo.RecordVO;
import cn.tedu.sgs.record.service.RecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/v1/record")
public class RecordController {

	@Autowired
	private RecordService recordService;

	@GetMapping("select")
	public JsonResult selectRecord(RecordQuery recordQuery) {
		log.debug("查询投放记录: {}", recordQuery);
		List<RecordVO> list = recordService.selectRecord(recordQuery);
		return JsonResult.ok(list);
	}

	@PostMapping("save")
	public JsonResult saveRecord(RecordSaveParam recordSaveParam) {
		log.debug("保存投放记录:{}", recordSaveParam);
		recordService.saveRecord(recordSaveParam);
		return JsonResult.ok();
	}

	@GetMapping("selectId")
	public JsonResult selectById(RecordQuery recordQuery) {
		log.debug("投放记录: {}", recordQuery);
		List<RecordVO> list = recordService.selectById(recordQuery);
		return JsonResult.ok(list);
	}

	@PostMapping("delete/{id}")
	public JsonResult deleteRecord(@PathVariable Long id) {
		log.debug("删除投放记录参数:{}", id);
		recordService.deleteRecord(id);
		return JsonResult.ok();
	}

	@PostMapping("approve/{id}")
	public JsonResult approveRecord(
			@PathVariable Long id,
			@RequestParam(required = false) Integer points) {
		log.debug("审核通过投放记录: {}, 增加积分: {}", id, points);
		RecordReviewDTO reviewDTO = new RecordReviewDTO();
		reviewDTO.setId(id);
		reviewDTO.setStatus(20); // 审核通过状态码
		reviewDTO.setPoints(points != null ? points : 0);
		recordService.reviewRecord(reviewDTO);
		return JsonResult.ok();
	}

	@PostMapping("reject/{id}")
	public JsonResult rejectRecord(@PathVariable Long id) {
		log.debug("审核拒绝投放记录: {}", id);
		RecordReviewDTO reviewDTO = new RecordReviewDTO();
		reviewDTO.setId(id);
		reviewDTO.setStatus(30); // 审核拒绝状态码
		recordService.reviewRecord(reviewDTO);
		return JsonResult.ok();
	}
}